package org.zjx.action.basic;

import org.zjx.action.Action;
import org.zjx.core.IWindow;
import org.zjx.entity.GameMouse;
import org.zjx.operation.OperationExecutor;
import org.zjx.state.GameState;
import org.zjx.util.ImageUtil;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.List;

public class ScreenshotAction extends Action {

    public ScreenshotAction(IWindow targetWindow) {
        super(targetWindow, false);
    }

    public ScreenshotAction(IWindow targetWindow, boolean requiresActiveWindow) {
        super(targetWindow, requiresActiveWindow);
    }

    @Override
    protected void doExecute() {
        GameState gameState = targetWindow.getState().getState(GameState.class);
        BufferedImage screenshot = targetWindow.execute(OperationExecutor::screenshot);
        gameState.setScreenshot(screenshot);

        GameMouse gameMouse = findMouse(screenshot);
        gameState.setGameMouse(gameMouse);
    }

    //region 查找游戏鼠标坐标

    /**
     * 查找游戏鼠标坐标
     */
    public GameMouse findMouse(BufferedImage screen) {
        GameMouse gameMouse = findOrdinaryMouse(screen);
        if (gameMouse != null) return gameMouse;
        gameMouse = findNpcMouse(screen);
        if (gameMouse != null) return gameMouse;
        gameMouse = findMonsterMouse(screen);
        if (gameMouse != null) return gameMouse;
        gameMouse = findWriteMouse(screen);
        if (gameMouse != null) return gameMouse;
        gameMouse = findFingerMouse(screen);
        return gameMouse;
    }

    private GameMouse findOrdinaryMouse(BufferedImage screen) {
        BufferedImage image = ImageUtil.stringToBufferedImage(GameMouse.MOUSE_IMAGE);
        java.util.List<Point> points = ImageUtil.matchTemplate(screen, image);
        if (!points.isEmpty()) {
            Point point = points.size() >= 2 ? points.get(1) : points.get(0);
            return new GameMouse(new Rectangle(point.x, point.y, image.getWidth(), image.getHeight()), GameMouse.Type.ORDINARY);
        }
        return null;
    }

    private GameMouse findNpcMouse(BufferedImage screen) {
        BufferedImage image = ImageUtil.stringToBufferedImage(GameMouse.MOUSE_NPC_IMAGE);
        java.util.List<Point> points = ImageUtil.matchTemplate(screen, image);
        if (!points.isEmpty()) {
            Point point = points.size() >= 2 ? points.get(1) : points.get(0);
            return new GameMouse(new Rectangle(point.x, point.y, image.getWidth(), image.getHeight()), GameMouse.Type.NPC);
        }
        return null;
    }

    private GameMouse findMonsterMouse(BufferedImage screen) {
        BufferedImage image = ImageUtil.stringToBufferedImage(GameMouse.MOUSE_MONSTER_IMAGE);
        java.util.List<Point> points = ImageUtil.matchTemplate(screen, image);
        if (!points.isEmpty()) {
            Point point = points.size() >= 2 ? points.get(1) : points.get(0);
            return new GameMouse(new Rectangle(point.x, point.y, image.getWidth(), image.getHeight()), GameMouse.Type.MONSTER);
        }
        return null;
    }

    private GameMouse findFingerMouse(BufferedImage screen) {
        BufferedImage image = ImageUtil.stringToBufferedImage(GameMouse.MOUSE_FINGER_IMAGE);
        java.util.List<Point> points = ImageUtil.matchTemplate(screen, image);
        if (!points.isEmpty()) {
            Point point = points.size() >= 2 ? points.get(1) : points.get(0);
            return new GameMouse(new Rectangle(point.x, point.y, image.getWidth(), image.getHeight()), GameMouse.Type.FINGER);
        }
        return null;
    }

    private GameMouse findWriteMouse(BufferedImage screen) {
        BufferedImage image = ImageUtil.stringToBufferedImage(GameMouse.MOUSE_WRITE);
        List<Point> points = ImageUtil.matchTemplate(screen, image);
        if (!points.isEmpty()) {
            Point point = points.size() >= 2 ? points.get(1) : points.get(0);
            return new GameMouse(new Rectangle(point.x, point.y, image.getWidth(), image.getHeight()), GameMouse.Type.WRITE);
        }
        return null;
    }
    //endregion
}
